package com.mc.ssm.book.controller;

import com.alibaba.fastjson.JSONObject;
import com.mc.ssm.base.BaseResponseJson;
import com.mc.ssm.book.model.Book;
import com.mc.ssm.book.service.BookService;
import com.mc.ssm.utils.EncodingTool;
import com.mc.ssm.utils.ErrorMessage;
import com.mc.ssm.utils.JsonUtil;
import org.apache.log4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhangzhenguo on 2017/2/11.
 */
@Controller
@RequestMapping("/book")
public class BookController {
    private Logger log = Logger.getLogger(BookController.class);

    @Resource
    private BookService bookService;


    @ResponseBody
    @RequestMapping("/addBook")
    public JSONObject saveBook(Book book) {
        int flag = bookService.saveBook(book);
        JSONObject jsonObject = new JSONObject();
        if (flag == 1) {
            jsonObject.put("flag", true);
            jsonObject.put("url", "book/showBook");
        } else {
            jsonObject.put("flag", false);
        }
        return jsonObject;
    }


    @ResponseBody
    @RequestMapping("/updateBook")
    public JSONObject updateBook(Book book) {
        int flag = bookService.updateBook(book);
        if (flag == 1) {
            return JsonUtil.dealJson(true, "book/showBook", "success");
        } else {
            return JsonUtil.dealJson(false, "", ErrorMessage.SYSTEM_ERROR);
        }
    }

    @RequestMapping("/showBook")
    public ModelAndView showBook() {
        log.info("查询所有book信息");
        List<Book> books = bookService.findAllBooks();
        return new ModelAndView("/pages/book/showBook", "bookList", books);
    }

    @RequestMapping(value = "/selectAllBooks", method = RequestMethod.GET)
    @ResponseBody
    public BaseResponseJson selectAllBooks() {
        List<Book> books = bookService.findAllBooks();
        BaseResponseJson responseJson = new BaseResponseJson();
        responseJson.setStatus(BaseResponseJson.CODE_SUCCESS);
        responseJson.setMsg("获取成功");
        responseJson.setData(books);
        System.out.print(books.toString());
        return responseJson;
    }

    @RequestMapping(value = "/findBookById",method = RequestMethod.GET)
    @ResponseBody
    public BaseResponseJson findBookById(@RequestParam(value = "bookId") long bookId){
        Book book = bookService.findBookById(bookId);
        BaseResponseJson responseJson = new BaseResponseJson();
        responseJson.setStatus(BaseResponseJson.CODE_SUCCESS);
        responseJson.setMsg("查询成功");
        responseJson.setData(book);
        System.out.print(book.toString());
        return responseJson;
    }

    @RequestMapping("/selectBookByName")
    public String selectBookByName(@RequestParam(value = "bookName") String bookName, Model model) {
        log.info("查询所有book信息" + bookName + EncodingTool.encodeStr(bookName));
        List<Book> books = bookService.selectBooksByName(bookName);
        model.addAttribute("bookList", books);
        return "showBook";
    }

    @RequestMapping("/home")
    public String home(Model model) {
        model.addAttribute("bookList", "list");
        return "home";
    }


}
